﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Data.Common;
using System.Data.SQLite;

namespace LightWeightORM
{
    namespace ORM
    {
        namespace TableAccessor
        {
            public enum ComparisonOperator
            { 
                Greater = 0,
                Less = 1,
                Equal = 2,
                Like = 3,
                GreaterOrEqual = 4,
                LessOrEqual = 5
            }

            /// <summary>
            /// Summary description for Filter
            /// </summary>
            public class Filter
            {

                public string ColumnName { get; set; }
                public object ColumnValue { get; set; }
                public ComparisonOperator ComparisonOperator { get; set; }

                private string _parameterName;
                public string ParameterName
                {
                    get 
                    {

                        if (_parameterName == string.Empty)
                            return _parameterName = this.ColumnName;
                        else
                            return _parameterName;
                    }
                    set { _parameterName = value; }
                }

                public Filter(string columnName, object columnValue, ComparisonOperator cpOperator)
                    : this(columnName, columnValue, string.Empty, cpOperator)
                { 
                    
                }
                public Filter(string columnName,object columnValue,string parameterName,ComparisonOperator cpOperator)
                {
                    this.ColumnName = columnName;
                    this.ColumnValue = columnValue;
                    this.ComparisonOperator = cpOperator;
                    this.ParameterName = parameterName;
                }

                /// <summary>
                /// 根据Filter返回Where条件子句
                /// </summary>
                /// <returns>Where条件子句</returns>
                public string ConditionExpression()
                {
                    string conditionExpression = string.Empty;

                    switch (this.ComparisonOperator)
                    {
                        case ComparisonOperator.Greater:
                            conditionExpression = string.Format("{0}>@{1}", this.ColumnName, this.ParameterName);
                            break;
                        case ComparisonOperator.Less:
                            conditionExpression = string.Format("{0}<@{1}", this.ColumnName, this.ParameterName);
                            break;
                        case ComparisonOperator.Equal:
                            conditionExpression = string.Format("{0}=@{1}", this.ColumnName, this.ParameterName);
                            break;
                        case ComparisonOperator.Like:
                            conditionExpression = string.Format("{0} LIKE :{1}", this.ColumnName, this.ParameterName);
                            break;
                        case ComparisonOperator.GreaterOrEqual:
                            conditionExpression = string.Format("{0}>=@{1}", this.ColumnName, this.ParameterName);
                            break;
                        case ComparisonOperator.LessOrEqual:
                            conditionExpression = string.Format("{0}<=@{1}", this.ColumnName, this.ParameterName);
                            break;
                        default:
                            break;
                    }

                    return conditionExpression;
                    
                }

                /// <summary>
                /// 根据Filter返回SQL Parameter
                /// </summary>
                /// <returns></returns>
                public DbParameter ToParameter()
                {

                    if (this.ComparisonOperator == ComparisonOperator.Like)
                    {
                        SQLiteParameter parms = new SQLiteParameter(":"+ this.ParameterName);
                        parms.Value = string.Format("%{0}%", this.ColumnValue);
                        return parms;
                    }
                    else
                    {
                        return new SQLiteParameter(this.ParameterName, this.ColumnValue);
                    }

                }
            }
        }
    }
}